
import zipfile
import shutil, os
from pathlib import Path
import datetime
import json

# # -*- coding: utf-8 -*-
import cv2
import numpy as np
from PIL import ImageFont, Image, ImageDraw
 
def get_font_render_size(text, fontsize):
    white = (255,255,255)
    canvas = Image.new('RGB', (2048,2048))
    draw = ImageDraw.Draw(canvas)
    monospace = ImageFont.truetype("msyh.ttc", fontsize)
    draw.text((0, 0), text, font=monospace, fill=white)
    bbox = canvas.getbbox()
    # 宽高
    size = (bbox[2] - bbox[0], bbox[3] - bbox[1])
    return size

# 生成一张图片
def create_pic(shape=[512, 256,1],val=0):
    width = shape[0]
    height = shape[1]
    c = shape[2]
    img = np.zeros([height,width , c], dtype=np.uint8)
    if val == 0:
        return img
    # 遍历每个像素点，并进行赋值
    for i in range(width):
        for j in range(height):
           img[i, j, :] = [val%256, val%256, val%256]

    return img

# 在图片中写入文字
def write_pic(image, strInfo, savePath='./xxx.bmp', text_size=80):
    # 获取图像的宽度和高度
    height, width = image.shape[:2]
    if 0:
        # 设置需要显示的字体
        # 定义字体、字号、颜色和厚度
        font = cv2.FONT_HERSHEY_SIMPLEX
        font_scale = 4
        color = (255, 255,255)
        thickness = 5
        # 获取要写入数字的宽度和高度
        text_size, _ = cv2.getTextSize(strInfo, font, font_scale, thickness)
        # 计算文本的位置，使其居中
        text_x = (width - text_size[0]) // 2
        text_y = (height + text_size[1]) // 2
        # cv在图像中写入数字
        cv2.putText(image, strInfo, (text_x, text_y), font, font_scale, color, thickness)
    else:
        #pil
        step = 10
        while 1:
            bbox = get_font_render_size(strInfo,text_size)
            text_w, text_H = bbox
            print(text_size,">>> ", bbox)
            if width-text_w<=0 or height-text_H <= 0:
                text_size -= 1
            elif width-text_w>2 or height-text_H>2:
                text_size += 1

            if (width-text_w>0 and height-text_H>0) and (width-text_w<step or height-text_H<step):
                print("success: ",text_size,">>> ", bbox)
                break
            
        text_x = (width - text_w) // 2
        text_y = (height - text_H) // 2
        #导入字体文件
        fontpath = "simsun.ttc"
        #设置字体的颜色
        b,g,r,a = 255,255,255,1
        #设置字体大小
        
        font = ImageFont.truetype(fontpath,text_size, encoding="utf-8")
        #将numpy array的图片格式转为PIL的图片格式
        print(image.shape)

        img_pil = Image.fromarray(image)
        #创建画板
        draw = ImageDraw.Draw(img_pil)
        #在图片上绘制中文
        
        draw.text((text_x, text_y),strInfo,font=font,fill=(b,g,r,a))
        #将图片转为numpy array的数据格式
        image = np.array(img_pil)
    cv2.imwrite(savePath, image)
    print("text x: ", text_x, " y: ", text_y)
    print("write path: ", savePath)
'''
def test1():
    path = "F:\\workespace\\z_projects\\woge-models\\xx\\mask-ink-edgeLackIgnore\\"
    for row in range(0,12):
        for col in range(0,10):
            thumbnail = create_pic(shape=[16384,21000])
            name = "sn_row"+str(row+1)+"col"+str(col+1)+"_0sn_.png"
            print(name)
            cv2.imwrite(path+name, thumbnail)

    exit(0)
'''
if __name__ == "__main__":
    #test1()
    info = "华星巡边"
    thumbnail = create_pic(shape=[512,512,3])
    write_pic(thumbnail, info+"--", "E:/thumbnail.bmp", 512//5)

